import fetch from 'node-fetch';

const yxyBaseAPI = 'https://api.yunxiyuanyxy.xyz/emoji/?list='
const yushengBaseUrl = 'http://api.botqsign.icu/'

export class meme extends plugin {
    constructor() {
        super({
            name: 'MEMZ表情包',
            dsc: '调用云溪院和瑜笙API发送表情包，后期可扩展',
            event: 'message',
            priority: 1000,
            rule: [{
                reg: '.*',
                fnc: 'sendMeme'
            }, ]
        });
        this.yxyEmojiMap = {
            '永雏塔菲': '永雏塔菲',
            '永雏': '永雏塔菲',
            '茧': '茧',
            '时雨': '时雨',
            '食欲': '时雨',
            '地球涩物': '地球涩物',
            '地球': '地球涩物',
            'ikun': 'ikun',
            '鸽鸽': 'ikun',
            '坤坤': 'ikun',
            '鲲鲲': 'ikun',
            'giegie': 'ikun',
            'vr猫猫': 'vr猫猫',
            'vr猫': 'vr猫猫',
            'vr喵': 'vr猫猫',
            'vr喵喵': 'vr猫猫',
            '乐': '乐',
            '樂': '乐',
            '亚托莉': '亚托莉',
            '东雪莲': '东雪莲',
            'Atri': '亚托莉',
            'atri': '亚托莉',
            '嘉心糖': '嘉心糖',
            "塞西莉亚": "塞西莉亚",
            "夜羽真白": "夜羽真白",
            "天王寺璃奈": "天王寺璃奈",
            "妹妹文学": "妹妹文学",
            "宇佐纪": "宇佐纪",
            "希儿": "希儿",
            "暹罗猫": "暹罗猫",
            "模块狗": "模块狗",
            "模块勾": "模块狗",
            "泉此方": "泉此方",
            "流萤": "流萤",
            "狩夜": "狩夜",
            "猫猫": "猫猫",
            "猫猫糕": "猫猫糕",
            "白咲": "白咲",
            "芙兰": "芙兰",
            "芙莉莲": "芙莉莲",
            "薄荷": "薄荷",
            "阿尼亚": "阿尼亚",
            "阿罗娜": "阿罗娜",
            "风又音理": "风又音理",
            "龙图": "龙图",
            "龙": "龙图",
            "fufu": "fufu",
            "夫夫": "fufu",
            "福福": "fufu",
            "初音": "fufu",
            "千绘莉": "千绘莉",
            "哆啦A梦": "哆啦A梦",
            "A梦": "哆啦A梦",
            "叮当猫": "哆啦A梦",
            "小叮当": "哆啦A梦",
            "大头": "大头",
            "小岛游": "小岛游",
            "明风": "明风",
            "柚子": "柚子",
            "柚子厨": "柚子",
            "柴郡": "柴郡",
            "随机柴郡": "柴郡",
            "随机柴郡": "柴郡",
            "波奇酱": "波奇酱",
            "爱莉希雅": "爱莉希雅",
            "珈百璃": "珈百璃",
            '咖波': '猫猫虫',
            'mmc': '猫猫虫',
            'kabo': '猫猫虫',
            'cappo': '猫猫虫',
            'cappo': '猫猫虫',
            '疾风鼬': '疾风鼬',
            '疾旋鼬': '疾风鼬',
            '旋疾鼬': '疾风鼬',
            '兄弟兄弟': '疾风鼬',
            '兄弟好香': '疾风鼬',
            '兄弟你好香': '疾风鼬',
            '兄弟我爱你': '疾风鼬',
            '程序员': '程序员',
            '写代码': '程序员',
            '写代码的': '程序员',
            '修电脑': '程序员',
            '修电脑的': '程序员',
            '破修电脑的': '程序员',
            'bug': '程序员',
            '四糸乃': '四糸乃',
            '四系乃': '四糸乃',
            'yoshino': '四糸乃',
            'Yoshino': '四糸乃',
            'yoshinon': '四糸乃',
            'Yoshinon': '四糸乃',
        };
        this.yushengEmojiMap = {
            '咖波': 'mmc',
            '随机咖波': 'mmc',
            'mmc': 'mmc',
            '猫猫虫': 'mmc',
            'cappo': 'mmc'
        };
    }

    async sendMeme(e) {
        const key = (e.msg || '').trim()
        const emojiMaps = [this.yxyEmojiMap, this.yushengEmojiMap]
        const fetchApis = [fetchYXYApi, fetchYSApi]

        const {
            val,
            fetchApiFunc
        } = emojiMaps.reduce((acc, map, index) => {
            const emoji = map[key]
            if (emoji) {
                return {
                    val: emoji,
                    fetchApiFunc: fetchApis[index]
                };
            }
            return acc;
        }, {
            val: null,
            fetchApiFunc: null
        });

        if (!val || !fetchApiFunc) {
            return false;
        }

        try {
            const imageSegment = segment.image(await fetchApiFunc(val))
            e.reply(imageSegment)
            return true
        } catch (err) {
            e.reply([`向服务器请求表情时出错啦，要不过会再来试试吧awa，错误原因${err}`])
            return false
        }
    }


}

/* util fncs */

// 获取云溪院API对应表情包
function fetchYXYApi(val) {
    return fetchMemeApi(val, yxyBaseAPI);
}

// 瑜笙API表情包
function fetchYSApi(val) {
    return fetchMemeApi(val, yushengBaseUrl);
}

const fetchMemeApi = async (val, baseUrl) => {
    if (!val) return null;
    const url = `${baseUrl}${val}`;

    try {
        const response = await fetch(url);
        return await response.buffer();
    } catch (error) {
        console.error('请求出错咧，错误原因：', error);
        throw error;
    }
};